#include <unistd.h>
#include <string.h>
#include <vector>
#include <map>
#include <sys/types.h>
#include <dirent.h>
#include "UacUtil.h"
#include "log.h"

namespace Usbstream{
bool UacUtil::getSoundInfo(set<string> &devicenodes, set<string> &cards){
	FILE *fp = NULL;
	char line[256] = {0};
	char member[512] ={0};
	//char devices[32] = {0};
	string cmd;

	//查看当前声卡的所有设备节点
	if(access(soundDevicePath, F_OK)){
		LOG_PrintError("not exist %s.\n", soundDevicePath);
		return false;
	}
	DIR* dp;
	struct dirent* ep;
	dp = opendir(soundDevicePath);
	if (dp == NULL)
	{	
		return false;		
	}
	while ((ep = readdir(dp))){
		string name = ep->d_name;
		devicenodes.insert(name);
	}
	closedir(dp);

	//查看声卡对应的文件，找出当前所有声卡的 usb端口和编号
	//查看文件是否存在
	if(access(cardsPath, F_OK)){
		LOG_PrintError("not exist %s.\n", cardsPath);
		return false;
	}
	fp = fopen(cardsPath, "r");
	if(!fp){
		LOG_PrintError("Fail to open %s.\n", cardsPath);
		return false;
	}
	//读取命令的返回值
#if 1
	while(fgets(line, sizeof(line), fp)){
		strcat(member, line);
		if(fgets(line, sizeof(line), fp)){
			strcat(member, line);
		}
		string cardsinfo = member;
		cards.insert(cardsinfo);
		memset(member, 0,sizeof(member));
	}
#endif
	fclose(fp);
	return true;
}

/**
 * 到文件中获取当前音频设备的支持的参数信息，包括format，channels，rates等
 * */
bool UacUtil::parseDeviceParam(int device_number, int mode, Json::Value &supportParam){
	vector<string> device_info;
	int info_index = -1;
	char params_file[64] = {0};
	FILE *fp = NULL;
	char line[256] = {0};
	string require_params[3] = {"Format", "Channels", "Rates"};
	//文件是否存在
	sprintf(params_file, deviceparams, device_number);
	if(access(params_file, F_OK)){
		LOG_PrintError("file %s is not exist\n", params_file);
		return false;
	}
	//打开文件
	fp = fopen(params_file, "r");
	if(fp == NULL){
		//打开失败直接返回
		LOG_PrintError("cannot open %s\n", params_file);
		return false;
	}
	//判断当前要获取的是输入音频还是输出音频的信息
	while(fgets(line, sizeof(line), fp)){
		device_info.push_back(line);
	}

	//判断当前要获取的是输入音频还是输出音频的信息
	string param_mark = mode ?  "Capture" : "Playback";

	//遍历读取的每一行数据,找到我们想要的数据的vector坐标
	for(int i = 0; i < (int)device_info.size(); i++){
		if(device_info[i].find(param_mark) != string::npos){
			info_index = i;
			break;
		}
	}
	if(info_index == -1){
		LOG_PrintError("cannot get %s info\n", param_mark.c_str());
		return false;
	}

	//从当前找到的行开始处理
	for(int i = info_index + 1; i < (int)device_info.size(); i++){
		if(device_info[i].length() == 1){
			break;
		}
		//循环判断是否存在我们想要的数据
		for(unsigned int j = 0; j < sizeof(require_params)/sizeof(string); j++){
			int index = device_info[i].find(require_params[j]);
			//LOG_PrintInfo("index:%d  ,device_info[i].npos:%d\n", index, device_info[i].npos);
			if(index == (int)device_info[i].npos){
				continue;
			}
			//处理数据将数据截取出来放入到对应的map中
			int start = index + require_params[j].length() + 2;
			string value = device_info[i].substr(start, device_info[i].length() - start - 1);
			supportParam[require_params[j]] = value;
			break;
		}
	}

#if 0
	map<string,string> temp;
	map<string,string>::iterator it;
	if(this -> m_mode){
		temp = this -> m_captureSupportParam;
	}else{
		temp = this -> m_playSupportParam;
	}
	LOG_PrintInfo("mode:%d  size:%d\n", m_mode, temp.size());
	for(it = temp.begin() ; it != temp.end(); it++){
		LOG_PrintInfo("%s --- %s   valuelength:%d\n", (it -> first).c_str(), (it -> second).c_str(), (it -> second).length());
	}
#endif
	return true;
}

}
